import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/views/Layout/index.vue'
import store from '@/store'
const routes = [
  {
    path: '/',
    component: Layout,
    // 子路由
    children: [
      { path: '/', component: () => import('@/views/Home/index.vue') },
      {
        path: '/category/:id',
        component: () => import('@/views/category/index.vue')
      },
      // 商品详情页
      {
        path: '/product/:id',
        component: () => import('@/views/Goods/index.vue')
      }
    ]
  },
  // 登录
  {
    path: '/login',
    component: () => import('@/views/Login/index.vue')
  },
  // qq授权登录页
  {
    path: '/login/callback',
    component: () => import('@/views/Login/callback')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 设置导航守卫
router.beforeEach((to, from, next) => {
  // 需要登录后才能访问的页面统统加上/member前缀,再判断有没有token,如果没有就退回登录页
  if (to.path.startsWith('/member') && !store.state.user.profile.token) {
    next('/login?redirectUrl=' + to.fullPath)
  } else {
    next()
  }
})
export default router
